import {Component, OnDestroy, OnInit} from '@angular/core';
import {Subscription} from "rxjs";
import {Router} from "@angular/router";
import {ToolsService} from "app/shared/service/tools/tools.service";
import {MatchService} from "../match.service";



@Component({
  selector: 'filter-page',
  templateUrl: './match-filter.component.html',
  styleUrls: ['./match-filter.component.scss']
})
export class MatchFilterComponent implements OnInit, OnDestroy {
  public event_type: any;
  public event_status: any;
  public citySelect: string;
  public cityId: number;
  public backUp: any;
  subscription: Subscription = new Subscription();

  constructor(public router: Router,
              public toolsService: ToolsService,
              public matchService: MatchService) {
    this.backUp = this.matchService.backUp;
  }

  eventType(index: number, formatType) {
    this.backUp.typeActive = index;
    this.matchService.backUp.formats = formatType;
    this.matchService.backUp.typeActive = index;
  }

  eventStatus(index: number, status) {
    this.backUp.statusActive = index;
    this.matchService.backUp.statusList = status;
    this.matchService.backUp.statusActive = index;
  }

  goBack() {
    this.router.navigate(['/match']);
    for (const key in this.matchService.saveSelected) {
      this.matchService.backUp[key] = this.matchService.saveSelected[key];
    }
  }

  filterSubmit() {
    const filterParam = this.matchService.filterParam;
    const saveSelected = this.matchService.saveSelected;
    for (const key in saveSelected) {
      saveSelected[key] = this.matchService.backUp[key];
    }
    filterParam.formats = saveSelected.formats;
    filterParam.statusList = saveSelected.statusList;
    filterParam.matchIpRecomId = saveSelected.matchIpRecomId;
    filterParam.cityId = this.matchService.currentCity.cityId;

    this.matchService.isFilter = true;
    this.router.navigate(['/match']);
  }

  matchFilterClum() {
    this.subscription.add(
      this.matchService.getMatchFilter().subscribe(res => {
        if (res.result === '0') {
          const whole = {
            "formatName": "全部",
            "formatType": null
          };
          this.event_type = res.data.matchFormatTypes;
          this.event_type.unshift(whole);
          this.event_status = res.data.matchStatus;
          this.matchService.event_recoms = res.data.matchIpRecoms;
          this.toolsService.hideLoading();
        }
      })
    );
  }

  ngOnInit() {
    this.toolsService.showLoading();
    this.matchFilterClum();
  }

  ngOnDestroy() {
    // 取消订阅
    this.subscription.unsubscribe();
    this.toolsService.hideLoading();
  }
}
